package niumao.minecraft.uhc.commands;

import niumao.minecraft.uhc.UHCGame;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class UnloadWorld implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (sender instanceof Player) {
            Player player = (Player) sender;
            if (!player.isOp()) {
                player.sendMessage("Only Op can execute this command!");
                return true;
            }
            if (cmd.getName().equalsIgnoreCase("unloadWorld")) {
                if (args.length == 1) {
                    String name = args[0];
                    if (name.equalsIgnoreCase("world")) {
                        player.sendMessage("Cannot unload this world.");
                        return true;
                    } else if (name.equalsIgnoreCase("uhc")) {
                        for (Player user : Bukkit.getOnlinePlayers())
                            user.teleport(new Location(Bukkit.getWorld("world"), 0, 4, 0));
                        UHCGame.unload();
                        player.sendMessage("Unload World successfully.");
                        return true;
                    } else {
                        World world = Bukkit.getWorld(name);
                        if (world == null) {
                            player.sendMessage("Cannot found this world.");
                            return true;
                        }
                        Bukkit.unloadWorld(world, false);
                        player.sendMessage("Unload World successfully.");
                    }
                    player.sendMessage("Cannot found this world.");
                    return true;
                }
                return false;
            }
        }
        sender.sendMessage(ChatColor.RED + "only Player can executor this command!");
        return true;
    }
}
